/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.alice.authoringtool.py.AliceStyle; // color AliceStyle.strBgColor

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor.CompositeElementEditor;
import edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor.CompositeElementPanel;

public class ResponsePropertyViewController extends PropertyViewController {

 protected javax.swing.JLabel responseLabel = new javax.swing.JLabel();
 protected edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel responsePanel = new edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel();
 protected edu.cmu.cs.stage3.alice.core.Element root = null;

 public ResponsePropertyViewController () {
  responseLabel.setOpaque( false );
  responsePanel.setOpaque( false );
  responsePanel.setLayout( new java.awt.BorderLayout() );
  responsePanel.setBorder( null );
  }

 public void set (edu.cmu.cs.stage3.alice.core.Property property, boolean allowExpressions, boolean omitPropertyName, final edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory) {
  super.set( property, true, allowExpressions, false, omitPropertyName, factory );
  setPopupEnabled( true );
  refreshGUI();
  }

 //Rem @Override
 protected java.awt.event.MouseListener getMouseListener () {
  return new java.awt.event.MouseAdapter() {
   //Rem @Override
   public void mousePressed (java.awt.event.MouseEvent ev) {
    // System.out.println("ResponsePropertyViewController.java: mousePressed: "+ev);
    ResponsePropertyViewController.this.popupButton.doClick();
    }
   };
  }

 public void setRoot (edu.cmu.cs.stage3.alice.core.Element root) {
  this.root = root;
  }

 //Rem @Override
 protected String getHTMLColorString (java.awt.Color color) {
  int r = color.getRed();
  int g = color.getGreen();
  int b = color.getBlue();
  return new String("#"+Integer.toHexString(r)+Integer.toHexString(g)+Integer.toHexString(b));
  }

 //Rem @Override
 public void getHTML (StringBuffer toWriteTo) {
  boolean isEnabled = false;
  if (property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.Behavior) {
   isEnabled = ((edu.cmu.cs.stage3.alice.core.Behavior)property.getOwner()).isEnabled.booleanValue();
   }
  String strikeStart = "";
  String strikeEnd = "";
  if (!isEnabled) {
   strikeStart = "<strike><font color=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTMLText"))+"\">";
   strikeEnd = "</font></strike>";
   }

  if (responsePanel.getComponentCount() > 0) {
   if (responsePanel.getComponent(0) instanceof CompositeElementPanel
     && property.get() instanceof edu.cmu.cs.stage3.alice.core.response.CompositeResponse) {
    CompositeElementPanel compPanel = (CompositeElementPanel)responsePanel.getComponent(0);
    int colSpan = CompositeElementEditor.getDepthCount(((edu.cmu.cs.stage3.alice.core.response.CompositeResponse)property.get()).componentResponses);
    compPanel.getHTML(toWriteTo, colSpan+1, true, !isEnabled);
   } else {
    java.awt.Color bgColor= responsePanel.getComponent(0).getBackground();
    if (!isEnabled) {
     bgColor = AuthoringToolResources.getColor("disabledHTML");
     }
    // #c0c0c0 -> AliceStyle.strBgColor
    toWriteTo.append("<tr>\n<td bgcolor="+getHTMLColorString(bgColor)+" style=\"border-left: 1px solid "+AliceStyle.strBgColor+
                     "; border-top: 1px solid "+AliceStyle.strBgColor+"; border-right: 1px solid "+AliceStyle.strBgColor+"; border-bottom: 1px solid "+AliceStyle.strBgColor+";\">"+strikeStart);
    toWriteTo.append(edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(responsePanel.getComponent(0)));
    toWriteTo.append(strikeEnd+"</td>\n</tr>\n");
    }
  } else {
   toWriteTo.append(strikeStart);
   super.getHTML(toWriteTo);
   toWriteTo.append(strikeEnd);
   }

  }


 //Rem @Override
 protected void updatePopupStructure () {
  popupStructure = edu.cmu.cs.stage3.alice.authoringtool.util.PopupMenuUtilities.makePropertyStructure( property, factory, includeDefaults, allowExpressions, includeOther, root );
  }

 //Rem @Override
 protected java.awt.Component getNativeComponent () {
  return responsePanel;
  }

 //Rem @Override
 protected Class getNativeClass () {
  return edu.cmu.cs.stage3.alice.core.Response.class;
  }

 //Rem @Override
 protected void updateNativeComponent () {
  responsePanel.removeAll();
  edu.cmu.cs.stage3.alice.core.Response response = (edu.cmu.cs.stage3.alice.core.Response)property.get();
  javax.swing.JComponent gui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getGUI( response );
  responsePanel.add( gui, java.awt.BorderLayout.CENTER );
  }

 }
